//
//  RegisterViewController.m
//  UI豆瓣
//
//  Created by dllo on 15/10/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterView.h"
#import "DataBaseUser.h"
#import "UserModle.h"
@interface RegisterViewController ()

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.registerV = [[RegisterView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view addSubview:self.registerV];
    [_registerV release];
    
    self.navigationItem.title = @"注册";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(regBtnAction:)];
    
    [[DataBaseUser shareDataBase] openDB];
    [[DataBaseUser shareDataBase] createTable];
}

- (void)regBtnAction:(UIBarButtonItem *)regBtn{
    
    //根据注册用户名查找数据库,如果返回的数组不为空则说明该用户已经被注册过,不能再注册,若不为空就可以注册
    NSMutableArray *userArr = [[DataBaseUser shareDataBase] selectUser:self.registerV.nameTextField.text];
    
    if (userArr.count != 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册" message:@"此用户名已经被注册过" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *canact = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            NSLog(@"取消");
        }];
        [alert addAction:canact];
        [self presentViewController:alert animated:YES completion:nil];
    }
    //如果注册的用户名或密码为空,则注册不成功
    else if ([self.registerV.nameTextField.text isEqualToString:@""] || [self.registerV.passwordTextField.text isEqualToString:@""]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册" message:@"未添加用户名或密码" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *canact = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            NSLog(@"取消");
        }];
        [alert addAction:canact];
        [self presentViewController:alert animated:YES completion:nil];
      }
    //如果两次输入的密码不正确,则注册不成功
    else if (![self.registerV.passwordTextField.text isEqualToString:self.registerV.okPasswordTextField.text]){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册" message:@"两次密码不相同" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *canact = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            NSLog(@"取消");
        }];
        [alert addAction:canact];
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    //注册成功
    else{
        
        
        self.name = self.registerV.nameTextField.text;
        self.password = self.registerV.passwordTextField.text;
        self.okPassword = self.registerV.okPasswordTextField.text;
        self.email = self.registerV.emailTextField.text;
        self.number = self.registerV.numberTextField.text;
        
        UserModle *user = [[UserModle alloc] init];
        user.userName = self.name;
        user.password = self.password;
        user.okPassword = self.okPassword;
        user.email = self.email;
        user.number = self.number;
        
        [[DataBaseUser shareDataBase] insertStudent:user];
        
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
